package com.yonyougov.devops.cruxcisupport.thread;

import com.yonyougov.devops.cruxcisupport.app.AppEntity;
import com.yonyougov.devops.cruxcisupport.app.AppService;
import com.yonyougov.devops.cruxcisupport.appenv.AppEnvEntity;
import com.yonyougov.devops.cruxcisupport.component.UserStore;
import com.yonyougov.devops.cruxcisupport.envversionhistory.EnvVersionHistoryEntity;
import com.yonyougov.devops.cruxcisupport.envversionhistory.EnvVersionHistoryService;
import com.yonyougov.devops.cruxcisupport.version.VersionEntity;
import com.yonyougov.devops.cruxcisupport.version.VersionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.List;

public class HandleEnvVersionHistoryThread extends Thread {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private List<AppEnvEntity> source;
    private UserStore userStore;
    private VersionService versionService;
    private AppService appService;
    private EnvVersionHistoryService envVersionHistoryService;

    public HandleEnvVersionHistoryThread(List<AppEnvEntity> source, VersionService versionService, UserStore userStore,
                                         EnvVersionHistoryService envVersionHistoryService, AppService appService) {
        this.source = source;
        this.versionService = versionService;
        this.userStore = userStore;
        this.envVersionHistoryService = envVersionHistoryService;
        this.appService = appService;
    }

    @Override
    public void run() {
        try {
            for (AppEnvEntity appEnvEntity : source) {
                VersionEntity versionEntity = new VersionEntity();
                versionEntity.setVersionName(appEnvEntity.getPackageVersion());
                versionEntity.setAppId(appEnvEntity.getAppId());
                VersionEntity version = versionService.findBy(versionEntity);

                AppEntity appEntity = new AppEntity();
                appEntity.setId(appEnvEntity.getAppId());
                AppEntity app = appService.findBy(appEntity);

                EnvVersionHistoryEntity envVersionHistoryEntity = new EnvVersionHistoryEntity();
                envVersionHistoryEntity.setEnvId(appEnvEntity.getEnvId());
                envVersionHistoryEntity.setAppId(appEnvEntity.getAppId());
                envVersionHistoryEntity.setAppName(app.getApp());
                envVersionHistoryEntity.setVersionName(appEnvEntity.getPackageVersion());
                envVersionHistoryEntity.setVersionId(version.getId());
                envVersionHistoryEntity.setModifyDate(new Date());
                envVersionHistoryEntity.setModifyBy(userStore.getCurrentUser().getId());
                envVersionHistoryService.add(envVersionHistoryEntity);
            }
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            logger.info("后台插入历史表 t_env_version_history 失败 : " + sw.toString());
        }
    }
}
